package com.it.test4case1;

import java.util.Random;

public class MyThread extends Thread {
    //共享数据
    //红包的金额，3个红包
    //最小红包数0.01
    static double money = 100;
    static int count = 3;
    static final double MIX = 0.01;
    public void run(){
        //同步代码块
        synchronized(MyThread.class){
            //判断是否有红包
            if(count == 0){
                System.out.println(getName()+"没有抢到红包");
            }else{
            //抢到了红包，随机红包的金额（第一次，第二次）
                double prize = 0;
                if(count == 1){
                    prize = money;
                }else{
                    //随机红包金额
                    Random r = new Random();
                    //最大红包范围，最坏的情况就是两个0.01，和99.98
                    double bounds = money-(count-1)*MIX;
                    prize = r.nextDouble(bounds);
                    //随机可能会随机到0，所以当prize<MIX的时候，直接强制为MIX
                    if(prize < MIX){
                        prize = MIX;
                    }
                }
                //从money当中，去掉当前中奖的金额
                money = money - prize;
                //红包的个数-1
                count--;
                System.out.println(getName()+"抢到了"+prize+"元");
            }
        }
    }
}
